<?php

namespace app\api\controller\product;

use think\Controller;
use think\Request;

class Cart extends Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->CartModel = model('Product.Cart');
    }

    public function getList()
    {
        $userid = $this->request->param('userid');
        $list = $this->CartModel->with(['product'])->where('userid', $userid)->select();
        // halt($list);
        if (empty($list)) {
            $this->error('暂无商品');
        }
        $this->success('查询成功', null, $list);
    }

    // 添加购物车
    public function add()
    {
        $userid = $this->request->param('userid');
        $proid = $this->request->param('proid');
        $num = $this->request->param('num');
        $total = $this->request->param('total');
        $action = $this->request->param('action');

        $product = model('Product.Product')->find($proid);
        if ($product['stock'] <= 0) {
            $this->error('该商品库存不足');
        }

        $Cart = $this->CartModel->where(['userid' => $userid, 'proid' => $proid])->find();
        $data = [
            'userid' => $userid,
            'proid' => $proid,
            'nums' => $num,
            'price' => $product['vip_price'] > 0 ? $product['vip_price'] : $product['price'],
            'total' => $product['vip_price'] > 0 ? $product['vip_price'] : $product['price'],
        ];

        // 立即购买
        if ($action == 'buynow') {
            $data['total'] = $total;
            $res = $this->CartModel->validate('common/Cart/Cart')->save($data);
            if ($res == false) {
                $this->error('添加购物车失败');
            }
            $this->success('添加购物车成功', null, $this->CartModel->id);
        }

        if ($Cart != null) {
            // 数量加1
            $data['nums'] = $data['nums'] + $Cart['nums'];
            // $data['nums'] = 3;

            $data['id'] = $Cart['id'];
            $res = $this->CartModel->isUpdate(true)->save($data);
            if ($res == false) {
                $this->error('添加购物车失败');
            }
            $this->success('添加购物车成功');
        }
        // echo '添加';
        $res = $this->CartModel->validate('common/Cart/Cart')->save($data);
        if ($res == false) {
            $this->error('添加购物车失败');
        }
        $this->success('添加购物车成功');
    }

    // 加1 / 减1
    public function onoption () {
        $id = $this->request->param('id');
        $num = $this->request->param('num');
        $pro = $this->CartModel->find($id);
        if (!$pro) {
            $this->error('该商品不存在');
        }
        $data = [
            'id' => $id,
            'userid' => $pro['userid'],
            'proid' => $pro['proid'],
        ];

        $data['nums'] = $pro['nums'] + $num;
        $data['total'] = $data['nums'] * $pro['price'];
        $res = $this->CartModel->isUpdate(true)->save($data);
        if ($res == false) {
            $this->error('操作错误');
        }
        $this->success('操作成功');
    }

    // 移除
    public function remove () {
        $id = $this->request->param('id');
        // halt($id);
        $pro = $this->CartModel->find($id);
        if (!$pro) {
            $this->error('该商品不存在');
        }
        $res = $this->CartModel->destroy($id);
        if ($res == false) {
            $this->error('删除失败');
        }
        $this->success('删除成功');
    }
}
